package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendLocalTemplateRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.WeiXinSinglePreviewRequest;
import com.fulihui.wgj.admin.integration.WxLocalTemplateServiceClient;
import com.fulihui.wgj.facade.request.template.LocalTemplateSendRequest;
import com.fulihui.wgj.facade.request.template.SinglePreviewRequest;
import com.fulihui.wgj.facade.service.template.WxLocalTemplateService;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @author: Levon
 * @date: 2018-07-26 15:56
 */
@Service
public class WxLocalTemplateServiceClientImpl implements WxLocalTemplateServiceClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxLocalTemplateServiceClientImpl.class);

    @Reference(version = "1.0.0")
    private WxLocalTemplateService wxLocalTemplateService;

    @Override
    public Result<Boolean> singlePreview(WeiXinSinglePreviewRequest request) {
        BaseResult baseResult = wxLocalTemplateService.singlePreview(BeanConv.copy(request, SinglePreviewRequest.class));
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<Boolean> send(SendLocalTemplateRequest request) {

        List<String> openids = Lists.newArrayList();
        //读取oss网络文件
        URL url = null;
        try {
            url = new URL(request.getFileUrl());
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String s;
            while ((s = reader.readLine()) != null) {
                openids.add(s);
            }
            reader.close();
        } catch (MalformedURLException e) {
            LOGGER.error("读取oss文件错误", e);
        } catch (IOException e) {
            LOGGER.error("读取oss文件错误", e);
        }

        if (CollectionUtils.isEmpty(openids)) {
            return JsonResultBuilder.fail(500, "未读取到数据，文件不能为空");
        }

        LocalTemplateSendRequest sendRequest = BeanConv.copy(request, LocalTemplateSendRequest.class);
        sendRequest.setOpenIds(openids);
        BaseResult baseResult = wxLocalTemplateService.sendTemplateMsgLocal(sendRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

}
